/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"C808C289-70D7-4E9F-A69F-FFDEB0782CE7",variableType:8}
 */
var fDolar = null;

/**
 * @type {String}
 *
 * @properties={typeid:35,uuid:"92FBC26B-8FF6-4BE6-B02A-3A62ED928EF3"}
 */
var fDescripcion = null;

/**
 * @type {Date}
 *
 * @properties={typeid:35,uuid:"DB3C48D9-30AA-4546-AE3F-7883F863575D",variableType:93}
 */
var fFechaAsiento = null;

/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"181D558E-716D-4BDF-8286-9C6D77F2F0F1",variableType:4}
 */
var fSubdiario = null;

/**
 * Callback method for when form is shown.
 *
 * @param {Boolean} firstShow form is shown first time after load
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"F9251F2E-1A7E-4A23-9C76-289C3C3AC51A"}
 */
function onShow(firstShow, event) {
	globals.gvarios = 1
	fDescripcion	= null
	fFechaAsiento	= null
	fSubdiario		= null
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"B470BC87-6391-42D3-988D-A88459BE3527"}
 */
function generar(event) {
	if (!globals.gFechaEnEjercicioActual(globals.gAnio, globals.gMes))
	{
		globals.DIALOGS.showWarningDialog('Aviso','El mes y año deben estar dentro del ejercicio actual','OK')
		return
	}
	if ((fFechaAsiento.getMonth()+1) != globals.gMes || fFechaAsiento.getFullYear() != globals.gAnio)
	{
		globals.DIALOGS.showWarningDialog('Aviso','La fecha del asiento debe estar dentro del mes y año elegidos','OK')
		return
	}
	databaseManager.setAutoSave(false)
	// se va a agrupar por cuenta_acum_id  en vez de cuentas_id  //  26/06/2013
	var query = 'SELECT cuentas_id, SUM(importe_debe) as "tot_debe", SUM (importe_haber) as "tot_haber" '+
	'FROM asiento_prov as "dtl" JOIN asiento_prov_cab as "cab" on dtl.asiento_cab_id = cab.asiento_cab_id '+
	'WHERE dtl.anio = ? and dtl.mes = ? and cab.subdiario_id = ? AND dtl.empresa = ? group by cuentas_id'
	
	/* se va a agrupar por cuenta_acum_id
	var query = 'SELECT cuentas_id, SUM(importe_debe) as "tot_debe", SUM (importe_haber) as "tot_haber" '+
	'FROM asiento_prov as "dtl" JOIN asiento_prov_cab as "cab" on dtl.asiento_cab_id = cab.asiento_cab_id '+
	'WHERE dtl.anio = ? and dtl.mes = ? and cab.subdiario_id = ? group by cuentas_id'
	*/
	var args = [globals.gAnio, globals.gMes, fSubdiario, globals.gUservID]
	
	var $ds = databaseManager.getDataSetByQuery('contable',query,args,-1)
	var cuantos = $ds.getMaxRowIndex()
	if (cuantos == 0)
	{
		globals.DIALOGS.showWarningDialog("Aviso",'No hay asientos con esas condiciones','OK')
		return
	}
	
	var query = 'SELECT marca_cierre, limbo FROM numcon WHERE anio = ? and mes = ? and empresa = ?'
	var args = new Array(globals.gAnio, globals.gMes, globals.gUservID)
	var $ds2 = databaseManager.getDataSetByQuery('contable',query,args,1)
	if ($ds2.getMaxRowIndex() == 0)
	{
		globals.DIALOGS.showWarningDialog('Aviso','Mes no habilitado','OK')
		return
	}
	var $cierre = $ds2.getValue(1,1)
	var $nro 	= $ds2.getValue(1,2)
	if ($cierre == 1)
	{
		globals.DIALOGS.showWarningDialog("Aviso",'Mes Cerrado','OK')
		return
	}
	
	/** @type {JSFoundSet<db:/contable/limbo_cab>}*/
	var $limbo = databaseManager.getFoundSet('contable','limbo_cab')
	
	$limbo.newRecord()
	$limbo.fecha 				= fFechaAsiento
	$limbo.moneda_carga 		= globals.gMonedaCarga
	$limbo.texto 				= fDescripcion
	$limbo.hizo         		= globals.glegajo
	$limbo.numero       		= $nro
	$limbo.provisorio   		= $nro
	$limbo.tipo_actualizacion 	= globals.z_conf_asiento_normal
	$limbo.moneda				= 'P'  // el provisorio es siempre en pesos

	var pfecha 		= new Date(globals.gAnio,globals.gMes-1,31)
    fDolar 			= globals.gBuscarCotizacion(pfecha)
    $limbo.dolar	= fDolar 
	
	var $total = 0
	var $dtl = $limbo.limbo_cab_to_limbo
	for (var i = 1 ; i <= cuantos ; i++)
	{
		var $debe 	= $ds.getValue(i,2)
		var $haber 	= $ds.getValue(i,3)
		if ($debe != null || $haber != null)
		{
			$dtl.newRecord()
			
			$dtl.leyenda 	= fDescripcion
			$dtl.cuentas_id = $ds.getValue(i,1)
			$dtl.numero 	= $nro
			$dtl.anio 		= globals.gAnio
			$dtl.mes 		= globals.gMes
			$dtl.centro   = globals.zconf_centro_default
			$dtl.division = globals.zconf_division_default

			
			if ($debe == null)
			{
				$dtl.columna 		= 'H'
				$dtl.importe_haber 	= $haber
				$dtl.importe 		= $haber
				/** @type {Number}*/
				var impodol_aux = $haber / fDolar
				$dtl.impodol = impodol_aux.toFixed(2)*1
				$total += $haber
			}
			else
			{
				$dtl.columna 		= 'D'
				$dtl.importe_debe 	= $debe
				$dtl.importe 		= $debe
				var impodol_aux = $debe / fDolar
				$dtl.impodol = impodol_aux.toFixed(2)*1
				if ($haber != null)
				{
					$dtl.newRecord()
					$dtl.leyenda 		= fDescripcion
					$dtl.cuentas_id 	= $ds.getValue(i,1)
					$dtl.numero 		= $nro
					$dtl.anio 			= globals.gAnio
					$dtl.mes 			= globals.gMes
					$dtl.columna 		= 'H'
					$dtl.importe_haber 	= $haber
					$dtl.importe 		= $haber
					$dtl.centro   		= globals.zconf_centro_default
					$dtl.division 		= globals.zconf_division_default
					var impodol_aux 	= $haber / fDolar
					$dtl.impodol 		= impodol_aux.toFixed(2)*1
					$total 				+= $haber
				}
			}
		}
	}
	$limbo.total = $total
	databaseManager.saveData()
	databaseManager.setAutoSave(true)
	globals.DIALOGS.showInfoDialog("Aviso",'El proceso termino exitosamente','OK')
}

/**
 * Handle changed data.
 *
 * @param {String} oldValue old value
 * @param {String} newValue new value
 * @param {JSEvent} event the event that triggered the action
 *
 * @returns {Boolean}
 *
 * @properties={typeid:24,uuid:"DAB3187E-0E94-4CA5-ACF8-182375556331"}
 */
function onDataChangeFecha(oldValue, newValue, event) {
	globals.gMes 	= newValue.substring(0,newValue.indexOf('|'))
	globals.gAnio	= newValue.substring(newValue.indexOf('|')+1,newValue.length)
	return true
}
